knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
#Echo = FALSE --> removes code chunks and only shows output
library(tidyverse)
library(here)
library(sf)
library(tmap)
#to intsall packages --> install.packages('packagename') in console
cmd-shift-enter shortcut for running the current code chunk
Create a code chunk: command-option-i for creating a code chunk
sf_trees <- read_csv(here("data", "sf_trees", "sf_trees.csv"))
#to see a summary of the dataset say summary(df) in the console
Example 1: Find counts of observations by legal_status and wrangle a bit.
### Method 1: group_by() %>% summarize()
sf_trees %>%
group_by(legal_status) %>%
summarize(tree_count = n())
## # A tibble: 10 × 2
## legal_status tree_count
## <chr> <int>
## 1 DPW Maintained 141725
## 2 Landmark tree 42
## 3 Permitted Site 39732
## 4 Planning Code 138.1 required 971
## 5 Private 163
## 6 Property Tree 316
## 7 Section 143 230
## 8 Significant Tree 1648
## 9 Undocumented 8106
## 10 <NA> 54
### Method 2: different way plus a few new functions
top_5_status <- sf_trees %>%
count(legal_status) %>%
drop_na(legal_status) %>% #drop rows containing missing values in the legal_status column, if you leave this empty then it will drop any rows that contain a missing value
rename(tree_count = n) %>%
relocate(tree_count) %>% #moves columns around
slice_max(tree_count, n = 5) %>% #this is taking the top 5 counts
arrange(desc(tree_count)) #organize by descending order,
#both of these methods do the same thing!
Make a graph of the top 5 from above
ggplot(data = top_5_status, aes(x= fct_reorder(legal_status, tree_count), y = tree_count)) + #fct_reorder takes the legal status results and reorders them according to smallest to largest tree count
geom_col(fill = 'darkgreen') + #fill changes the color of the columns
labs(x = 'Legal Status', y = 'Tree Count') +
coord_flip() + #flips so columns go from vertical alignment to horizontal alginment
theme_minimal()
Example 2: Only going to keep observations where legal status is “Permitted Site” and caretaker is “MTA”, and store as permitted_data_df
shift-cmd-c to comment/uncomment quickly
# sf_trees$legal_status %>% unique()
# unique(sf_trees$caretaker)
permitted_data_df <- sf_trees %>%
filter(legal_status %in% c('Permitted Site', 'Private') & caretaker %in% "MTA") #the row has to match all of the following arguments (if using a comma, for AND use &, for OR use |)
#remember if you use filter with == c('a', 'b') it will look for a and then b, so in this case you have to use:
#filter(legal_status %in% c('Permitted Site', 'Private'))
Example 3: Only keep Blackwood Acacia trees, and then only keep columns legal_status, date, latitude, longitude and store as blackwood_acacia_df
blackwood_acacia_df <- sf_trees %>%
filter(str_detect(species, "Blackwood Acacia")) %>% #str_detect looks through the strings to see if any of the string is detected (aka if the string has 'my name is Sophia' you can use str_detect to search only for Sophia)
select(legal_status, date, lat = latitude, lon = longitude) #also renaming columns
### Make a little graph of locations
ggplot(data = blackwood_acacia_df, aes(x=lon, y=lat)) +
geom_point(color = 'darkgreen')
Example 4: use tidyr::separate() Using separate to use the :: to separate the common name from the scientific name
sf_trees_sep <- sf_trees %>%
separate(species, into = c('spp_scientific', 'spp_common'), sep = " :: ")
#this is separating using the :: and renaming the two columns as listed above
** Example 5** use tidyr::unite()
ex_5 <- sf_trees %>%
unite("id_status", tree_id, legal_status, sep = "_COOL_")
#this is uniting in a new column "id_status"
# it is dropping the columns
Using sf package and tmap
Step 1: convert the lat/lon to spatial point, using st_as_sf()
#this is converting to a geometry so it knows it is a spatial object
blackwood_acacia_sf <- blackwood_acacia_df %>%
drop_na(lat,lon) %>%
st_as_sf(coords = c('lon', 'lat'))
### we need to tell R what the coordinate reference is
st_crs(blackwood_acacia_sf) <-4326 #this is the WGS ESG 84 (classic)
ggplot(data = blackwood_acacia_sf) +
geom_sf(color = "darkgreen") +
theme_minimal()
Read in the SF shapefile and add to map
sf_map <- read_sf(here("data", "sf_map", "tl_2017_06075_roads.shp"))
sf_map_transform <- st_transform(sf_map,4326)
ggplot(data = sf_map_transform) +
geom_sf()
Lets combine the maps!
ggplot() +
geom_sf(data = sf_map,
size = 0.1,
color = 'darkgrey')+
geom_sf(data = blackwood_acacia_sf,
color = 'red',
size = 0.5) +
theme_void() +
labs(title = "blackwood acacias in SF")
tmap_mode('view')
tm_shape(blackwood_acacia_sf) +
tm_dots()